/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lzh.compiler.parcelerdemo;

import com.lzh.compiler.parceler.Parceler;
import com.lzh.compiler.parceler.annotation.FastJsonConverter;
import com.lzh.compiler.parcelerdemo.base.BaseAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;

/**
 * MainAbility.
 */
public class MainAbility extends BaseAbility {
    private Button toInjectorActivity;
    private Button toBundleActivity;
    private Button toIntentActivity;
    private Button toTestCastActivity;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        Parceler.setDefaultConverter(FastJsonConverter.class);

        getInitData();
    }

    private void getInitData() {
        if (findComponentById(ResourceTable.Id_toInjectorActivity) instanceof Button) {
            toInjectorActivity = (Button) findComponentById(ResourceTable.Id_toInjectorActivity);
        }
        if (findComponentById(ResourceTable.Id_toBundleActivity) instanceof Button) {
            toBundleActivity = (Button) findComponentById(ResourceTable.Id_toBundleActivity);
        }
        if (findComponentById(ResourceTable.Id_toIntentActivity) instanceof Button) {
            toIntentActivity = (Button) findComponentById(ResourceTable.Id_toIntentActivity);
        }
        if (findComponentById(ResourceTable.Id_to_test_compat) instanceof Button) {
            toTestCastActivity = (Button) findComponentById(ResourceTable.Id_to_test_compat);
        }
        toInjectorActivity.setClickedListener(component -> {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName("com.lzh.compiler.parcelerdemo")
                    .withAbilityName("com.lzh.compiler.parcelerdemo.InjectorAbility")
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        });
        toBundleActivity.setClickedListener(component -> {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName("com.lzh.compiler.parcelerdemo")
                    .withAbilityName("com.lzh.compiler.parcelerdemo.BundleAbility")
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        });
        toTestCastActivity.setClickedListener(component -> {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName("com.lzh.compiler.parcelerdemo")
                    .withAbilityName("com.lzh.compiler.parcelerdemo.TestCastAbility")
                    .build();
            intent.setOperation(operation);
            startAbility(intent);
        });
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
    }
}

